package com.easondongh.blog.controller;

import com.easondongh.blog.enums.ResultEnum;
import com.easondongh.blog.pojo.Comment;
import com.easondongh.blog.pojo.CommentGoods;
import com.easondongh.blog.pojo.User;
import com.easondongh.blog.service.CommentService;
import com.easondongh.blog.utils.Page;
import com.easondongh.blog.utils.Result;
import com.easondongh.blog.utils.ShiroUtils;
import com.easondongh.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/4/8 15:01
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 新增评论
     * @param comment
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<ResultEnum> save(@RequestBody Comment comment) {
        this.commentService.save(comment);
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 获取博客id下的所有评论
     * @param id
     * @return
     */
    @RequestMapping(value = "/getListByBlogId/{id}", method = RequestMethod.GET)
    public Result<List<Comment>> getListByBlogId(@PathVariable("id") String id) {
        List<Comment> commentList = this.commentService.getListByBlogId(id);
        return new Result<>(commentList);
    }

    /**
     * 给评论点赞
     * @param commentGoods
     * @return
     */
    @RequestMapping(value = "/good", method = RequestMethod.POST)
    public Result<ResultEnum> good(@RequestBody CommentGoods commentGoods) {
        if(StringUtils.isNotBlank(commentGoods.getCommentId())) {
            User loginUser = (User) ShiroUtils.getLoginUser();
            if(loginUser != null) {
                commentGoods.setUserId(loginUser.getUserId());
                this.commentService.good(commentGoods);
            }
        }
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 判断当前用户是否已给当前评论点赞
     * @param commentId
     * @return
     */
    @RequestMapping(value = "/hasGood/{id}", method = RequestMethod.GET)
    public Result<Boolean> hasGood(@PathVariable("id") String commentId) {
        boolean hasGood = false;
        if(StringUtils.isNotBlank(commentId)) {
            User loginUser = (User) ShiroUtils.getLoginUser();
            if(loginUser != null) {
                hasGood = this.commentService.hasGoodByCommentAndUser(loginUser.getUserId(), commentId);
            }
        }
        return new Result<>(hasGood);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public Result<Page<Comment>> getList(@RequestBody Page<Comment> page) {
        page = commentService.getByPage(page);
        return new Result<>(page);
    }

    /**
     * 后台分页查询
     * @param page
     * @return
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<Comment>> getByPage(@RequestBody Page<Comment> page) {
        page = commentService.getByPageBack(page);
        return new Result<>(page);
    }

    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    public Result<ResultEnum> deleteById(@PathVariable("id") String id) {
        this.commentService.deleteById(id);
        return new Result<>(ResultEnum.SUCCESS);
    }

}
